package c03.locksupport;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.LockSupport;

import static util.Sleeper.sleep;

@Slf4j(topic = "c.LockSupportParkTest")
public class LockSupportParkTest {

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            log.debug("start t1...");
            log.debug("thread t1 初始打断状态：{}", Thread.currentThread().isInterrupted());
            LockSupport.park(); // t1会在此处等待
            log.debug("执行t1.interrupt()后，打断状态：{}", Thread.currentThread().isInterrupted());
            // 设置了打断标记以后，t1线程就不会被park住了
            LockSupport.park();
            log.debug("end t1...");
        }, "t1");
        t1.start();

        sleep(1);
        /*
        当被打断线程正在sleep,wait,join时，会导致被打断线程抛出InterruptedException,并清除打断标记
        当打断的线程处在运行状态时，则会设置打断标记
        park的线程被打断也会设置打断标记
         */
        t1.interrupt(); // t1线程被park住了，打断会设置打断标记
    }
}
